cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _gameover: null,
        _gameresult: null,
        _seats: [],
        _isGameEnd: false,
        _pingju: null,
        _win: null,
        _lose: null,
    },

    // use this for initialization
    onLoad: function () {
        if (cc.vv == null) {
            return;
        }
        if (cc.vv.gameNetMgr.conf == null) {
            return;
        }

        if (cc.vv.gameNetMgr.conf.type) {
            this._gameover = this.node.getChildByName("game_over");
            this._gameover.active = false;
        }

        if (cc.vv.gameNetMgr.conf.type == "mj") {
            this.mjLoad();
        } else if (cc.vv.gameNetMgr.conf.type == "nn" || cc.vv.gameNetMgr.conf.type == "jdb") {
            this.pkLoad();
        }

        this._btnClose = this._gameover.getChildByName("btnClose");
        this._btnShare = this._gameover.getChildByName("btnShare");

        this._listRoot = this._gameover.getChildByName("result_list");
        for (var i = 1; i <= cc.vv.gameNetMgr.seats.length; ++i) {
            var s = "s" + i;
            var sn = this._listRoot.getChildByName(s);
            var viewdata = {};
            viewdata.username = sn.getChildByName('seat').getChildByName('name').getComponent(cc.Label);
            viewdata.zhuang = sn.getChildByName('seat').getChildByName('zhuang');
            viewdata.allscore = sn.getChildByName('allscore').getComponent(cc.Label);
            viewdata.score = sn.getChildByName('score').getComponent(cc.Label);

            if (cc.vv.gameNetMgr.conf.type == "mj") {
                viewdata.reason = sn.getChildByName('reason').getComponent(cc.Label);
                viewdata.hupai = sn.getChildByName('hupai');
                viewdata.hupaifen = sn.getChildByName('hupaifen').getComponent(cc.Label);
                viewdata.hu = sn.getChildByName('hu');
                viewdata.mahjongs = sn.getChildByName('pai');
                var f = sn.getChildByName('fan');
                if (f != null) {
                    viewdata.fan = f.getComponent(cc.Label);
                }
                viewdata._pengandgang = [];
            }

            if (cc.vv.gameNetMgr.conf.type == "nn" || cc.vv.gameNetMgr.conf.type == "jdb") {
                viewdata.erguo = sn.getChildByName('erguo').getComponent(cc.Label);
                viewdata.duimen = sn.getChildByName('duimen').getComponent(cc.Label);
                viewdata.sifan = sn.getChildByName('sifan').getComponent(cc.Label);
            }
            this._seats.push(viewdata);
        }

        var btnClose = cc.find("Canvas/game_over/btnClose");
        if (btnClose) {
            cc.vv.utils.addClickEvent(btnClose, this.node, "GameOver", "onBtnCloseClicked");
        }

        var btnShare = cc.find("Canvas/game_over/btnShare");
        if (btnShare) {
            cc.vv.utils.addClickEvent(btnShare, this.node, "GameOver", "onBtnShareClicked");
        }

        //初始化网络事件监听器
        var self = this;
        this.node.on('game_over', function (data) {
            console.log('-----------------hu')
            if (cc.vv.gameNetMgr.conf.type == "mj") {
                self.onGameMJOver(data.detail);
            }
        });

        this.node.on('game_pk_over', function (data) {
            if (cc.vv.gameNetMgr.conf.type == "nn" || cc.vv.gameNetMgr.conf.type == "jdb") {
                self.onGameNNOver(data.detail);
            }
        });

        this.node.on('game_end', function (data) {
            self._isGameEnd = true;
            self._btnClose.active = true;
            self._btnShare.active = true;
        });

    },
    mjLoad: function () {
        this._pingju = this._gameover.getChildByName("pingju");
        this._win = this._gameover.getChildByName("win");
        this._lose = this._gameover.getChildByName("lose");
        this._hupaiType = this._gameover.getChildByName('hupaiType').getComponent(cc.Label);
    },
    pkLoad: function () {

    },
    onGameOver(data) {
        if (cc.vv.gameNetMgr.conf.type == "mj") {
            this.onGameMJOver(data);
        } else if (cc.vv.gameNetMgr.conf.type == "nn") {
            this.onGameNNOver(data);
        } else if (cc.vv.gameNetMgr.conf.type == "jdb") {
            this.onGameJDBOver(data);
        }
    },

    onGameMJOver(data) {
        this._pingju.active = false;
        this._win.active = false;
        this._lose.active = false;
        this._btnClose.active = false;
        this._btnShare.active = false;

        var myscore = data[cc.vv.gameNetMgr.seatIndex].score;
        if (myscore > 0) {
            this._win.active = true;
        }
        else if (myscore < 0) {
            this._lose.active = true;
        }
        else {
            this._pingju.active = true;
        }


        //显示玩家信息
        for (var i = 0; i < 4; ++i) {
            var seatView = this._seats[i];
            var userData = data[i];
            var hued = false;

            seatView.username.string = cc.vv.gameNetMgr.seats[i].name;
            seatView.zhuang.active = cc.vv.gameNetMgr.button == i;

            //胡牌的玩家才显示 是否清一色 根xn的字样
            var numOfGangs = userData.angangs.length + userData.wangangs.length + userData.diangangs.length;
            var numOfGen = userData.numofgen;
            var actionArr = [];
            var is7pairs = false;
            // var ischadajiao = false;
            for (var j = 0; j < userData.actions.length; ++j) {
                var ac = userData.actions[j];

                if (ac.type == "hu") {
                    actionArr.push("胡牌");
                    this._hupaiType.string = userData.pattern;
                } else if (ac.type == "fang pao") {
                    actionArr.push("放炮");
                }
            }

            for (var o = 0; o < 3; ++o) {
                seatView.hu.children[o].active = false;
            }
            if (userData.huorder >= 0) {
                seatView.hu.children[userData.huorder].active = true;
            }
            seatView.reason.string = actionArr.join("、");


            if (userData.score > 0) {
                seatView.score.string = "+" + userData.score;
                if (userData.baolen == 0) {
                    seatView.fan.string = 0;
                } else {
                    seatView.fan.string = "+" + userData.baolen * 10 * 3;
                }
                seatView.hupaifen.string = "+" + (userData.score - userData.baolen * 10 * 3);
            }
            else {
                seatView.score.string = userData.score;
                if (userData.baolen == 0) {
                    seatView.fan.string = 0;
                } else {
                    seatView.fan.string = "-" + userData.baolen * 10;
                }
                seatView.hupaifen.string = userData.score + userData.baolen * 10;
            }

            seatView.allscore.string = userData.totalscore > 0 ? "+" + userData.totalscore : userData.totalscore;

            var hupai = -1;
            if (hued) {
                hupai = userData.holds.pop();
            }

            cc.vv.mahjongmgr.sortMJ(userData.holds);//, userData.dingque

            //胡牌不参与排序
            if (hued) {
                userData.holds.push(hupai);
            }

            //隐藏所有牌
            for (var k = 0; k < seatView.mahjongs.childrenCount; ++k) {
                var n = seatView.mahjongs.children[k];
                n.active = false;
            }

            var lackingNum = (userData.pengs.length + numOfGangs + userData.chis.length) * 3;

            //显示相关的牌
            for (var k = 0; k < userData.holds.length; ++k) {
                var pai = userData.holds[k];
                var n = seatView.mahjongs.children[k + lackingNum];
                n.active = true;
                var sprite = n.getComponent(cc.Sprite);
                sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("M_", pai);
            }


            for (var k = 0; k < seatView._pengandgang.length; ++k) {
                seatView._pengandgang[k].active = false;
            }

            //初始化杠牌
            var index = 0;
            var gangs = userData.angangs;
            for (var k = 0; k < gangs.length; ++k) {
                var mjid = gangs[k];
                this.initPengAndGangs(seatView, index, mjid, "angang");
                index++;
            }

            var gangs = userData.diangangs;
            for (var k = 0; k < gangs.length; ++k) {
                var mjid = gangs[k];
                this.initPengAndGangs(seatView, index, mjid, "diangang");
                index++;
            }

            var gangs = userData.wangangs;
            for (var k = 0; k < gangs.length; ++k) {
                var mjid = gangs[k];
                this.initPengAndGangs(seatView, index, mjid, "wangang");
                index++;
            }

            //初始化碰牌
            var pengs = userData.pengs
            if (pengs) {
                for (var k = 0; k < pengs.length; ++k) {
                    var mjid = pengs[k];
                    this.initPengAndGangs(seatView, index, mjid, "peng");
                    index++;
                }
            }
            //初始化吃牌
            var chis = userData.chis
            if (chis) {
                for (var k = 0; k < chis.length; ++k) {
                    var mjid = chis[k];
                    this.initPengAndGangs(seatView, index, mjid, "chi");
                    index++;
                }
            }
        }
    },

    onGameNNOver(data) {
        this._gameover.active = true;
        for (var i = 0; i < 10; ++i) {
            var seatView = this._seats[i];
            var userData = data[i];
            seatView.username.string = cc.vv.gameNetMgr.seats[i].name;
            seatView.zhuang.active = cc.vv.gameNetMgr.button == i;
            seatView.erguo.string = "二过:" + userData.erguo;
            seatView.duimen.string = "对门:" + userData.duimen;
            seatView.sifan.string = "四反:" + userData.sifan;
            seatView.score.string = "得分:" + userData.score > 0 ? "+" + userData.score : userData.score;
            seatView.allscore.string = "总计:" + userData.totalscore > 0 ? "+" + userData.totalscore : userData.totalscore;
        }
    },

    initPengAndGangs: function (seatView, index, mjid, flag) {
        var pgroot = null;
        if (seatView._pengandgang.length <= index) {
            pgroot = cc.instantiate(cc.vv.mahjongmgr.pengPrefabSelf);
            seatView._pengandgang.push(pgroot);
            seatView.mahjongs.addChild(pgroot);
        }
        else {
            pgroot = seatView._pengandgang[index];
            pgroot.active = true;
        }

        var sprites = pgroot.getComponentsInChildren(cc.Sprite);
        for (var s = 0; s < sprites.length; ++s) {
            var sprite = sprites[s];
            if (sprite.node.name == "gang") {
                var isGang = flag != "peng";
                sprite.node.active = isGang;
                sprite.node.scaleX = 1.0;
                sprite.node.scaleY = 1.0;
                if (flag == "angang") {
                    sprite.spriteFrame = cc.vv.mahjongmgr.getEmptySpriteFrame("myself");
                    sprite.node.scaleX = 1.4;
                    sprite.node.scaleY = 1.4;
                }
                else if (flag == "chi" || flag == "peng") {
                    sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("B_", "");
                }
                else {
                    sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("B_", mjid);
                }
            }
            else {
                if (flag == "chi") {
                    sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("B_", mjid[s]);
                } else {
                    sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("B_", mjid);
                }
            }
        }
        pgroot.x = index * 55 * 3 + index * 24;
    },

    onBtnCloseClicked: function () {
        cc.director.loadScene("hall");
    },

    onBtnShareClicked: function () {
        cc.vv.anysdkMgr.shareResult();
    },

    onBtnReadyClicked: function () {
        console.log("onBtnReadyClicked");
        if (!this._isGameEnd && cc.vv.gameNetMgr.conf.type == "mj") {
            cc.vv.net.send('ready');
        }
        this._gameover.active = true;
    },

    onBtnShareClicked: function () {
        console.log("onBtnShareClicked");
    }

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
